
package org.reliablesource.prism.product.workbench;

import java.net.URL;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.reliablesource.prism.ui.workbench.actions.AnimateAction;

/**
 * Builds the actions and populates the menubar and toolbar when a new window is
 * opened. This work is factored into a separate class to avoid cluttering the
 * advisor.
 */
public class PrismActionBarAdvisor extends ActionBarAdvisor {

	protected IWorkbenchAction quitAction, aboutAction, deleteContactAction,
			addContactAction, loginAction, preferencesAction;

	protected IWorkbenchAction addGroupAction, chatAction, messageAction,
			renameAction, openIntroAction;

	protected IAction hideEditorArea, toggleToolbar, 	toggleStatusLine;

	private AnimateAction newConnectionAction;

	protected Action jobAction;

	private final PrismWorkbenchWindowAdvisor windowAdvisor;

	public PrismActionBarAdvisor(
			PrismWorkbenchWindowAdvisor windowAdvisor,
			IActionBarConfigurer configurer) {
		super(configurer);
		this.windowAdvisor = windowAdvisor;
	}

	protected void makeActions(IWorkbenchWindow window) {
		quitAction = ActionFactory.QUIT.create(window);
		register(quitAction);
		openIntroAction = ActionFactory.INTRO.create(window);

		aboutAction = ActionFactory.ABOUT.create(window);
		deleteContactAction = ActionFactory.DELETE.create(window);
		register(deleteContactAction);
		renameAction = ActionFactory.RENAME.create(window);
		register(renameAction);

		toggleToolbar = new Action("Toolbar", IAction.AS_CHECK_BOX) {
			public void run() {
				windowAdvisor.setShowToolbar(!windowAdvisor.getShowToolbar());
				updateEnablements();
			}
		};

		toggleStatusLine = new Action("Status Line", IAction.AS_CHECK_BOX) {
			public void run() {
				windowAdvisor.setShowStatusline(!windowAdvisor
						.getShowStatusline());
				updateEnablements();
			}
		};

		jobAction = new Action("Start Long Operation") {
			public void run() {
				Job job = new Job("Working") {
					protected IStatus run(IProgressMonitor monitor) {
						monitor.beginTask("Calculating things", 1000);
						for (int i = 0; i < 1000; i++) {
							monitor.worked(1);
							if (monitor.isCanceled())
								return Status.CANCEL_STATUS;
							try {
								Thread.sleep(10);
								monitor.subTask("task" + i);
							} catch (InterruptedException e) {
							}
						}
						monitor.done();
						return Status.OK_STATUS;
					}
				};
				job.setUser(true);
				job.schedule();
			}
		};

		hideEditorArea = new Action("&Hide Chat Area") {
			public void run() {
				getActionBarConfigurer().getWindowConfigurer().getWindow()
						.getActivePage().setEditorAreaVisible(false);
			}

			public boolean isEnabled() {
				return true;
			}
		};

	}

	protected void fillMenuBar(IMenuManager menuBar) {
		IMenuManager fileMenu = new MenuManager("&Hyperbola", "file");
		menuBar.add(fileMenu);
		fileMenu.add(loginAction);
		fileMenu.add(new Separator());
		fileMenu.add(chatAction);
		fileMenu.add(new Separator());
		fileMenu.add(quitAction);

		IMenuManager contactsMenu = new MenuManager("&Contacts", "contacts");
		menuBar.add(contactsMenu);
		contactsMenu.add(addContactAction);
		contactsMenu.add(addGroupAction);
		contactsMenu.add(deleteContactAction);

		IMenuManager viewMenu = new MenuManager("&View", "view");
		menuBar.add(viewMenu);
		viewMenu.add(toggleToolbar);
		viewMenu.add(toggleStatusLine);

		IMenuManager toolsMenu = new MenuManager("&Tools", "tools");
		menuBar.add(toolsMenu);
		toolsMenu.add(hideEditorArea);
		toolsMenu.add(new Separator());
		toolsMenu.add(preferencesAction);

		IMenuManager helpMenu = new MenuManager("&Help", "help");
		menuBar.add(helpMenu);
		helpMenu.add(openIntroAction);
		helpMenu.add(new Separator());
		helpMenu.add(aboutAction);
	}

	protected void fillCoolBar(ICoolBarManager mainCoolBar) {
		ToolBarManager toolbar = new ToolBarManager(SWT.FLAT);
		toolbar.add(loginAction);
		toolbar.add(addContactAction);
		toolbar.add(new Separator());
		toolbar.add(chatAction);
		toolbar.add(messageAction);
		ToolBarManager searchToolbar = new ToolBarManager(SWT.FLAT);
//		searchToolbar.add(
//				new SearchContributionItem(new Action() {
//			public void run() {
//				MessageDialog.openWarning(getActionBarConfigurer()
//						.getWindowConfigurer().getWindow().getShell(),
//						"Warning", "Not Implemented Yet!");
//			}
//		}, "Search Contacts"));

		mainCoolBar.add(toolbar);
		mainCoolBar.add(searchToolbar);
	}

	protected void fillStatusLine(IStatusLineManager statusLine) {
//		StatusLineProgressProvider progressProvider = (StatusLineProgressProvider) windowAdvisor
//				.getData(PrismWorkbenchAdvisor.PROGRESS_PROVIDER_KEY);
//		if (progressProvider != null) {
//			Image stillImage = createImage("icons/jobs/Throbber-Off.gif")
//					.createImage();
//			URL url = Platform.find(Platform
//					.getBundle("org.eclipsercp.hyperbola.ui.workbench"),
//					new Path("icons/jobs/Throbber.gif"));
//			AnimatingStatusLineContribution item = null;
//			item = new AnimatingStatusLineContribution(url, stillImage);
//			if (item != null) {
//				progressProvider.add(item);
//				progressProvider.add(statusLine);
//				statusLine.appendToGroup(StatusLineManager.MIDDLE_GROUP, item);
//			}
//		}
	}

	public static ImageDescriptor createImage(String location) {
		URL url = Platform.find(Platform
				.getBundle("org.reliablesource.prism.ui.workbench"), new Path(
				location));
		return ImageDescriptor.createFromURL(url);
	}

	public void postWindowCreate() {
		updateEnablements();
	}

	private void updateEnablements() {
		toggleToolbar.setChecked(windowAdvisor.getShowToolbar());
		toggleStatusLine.setChecked(windowAdvisor.getShowStatusline());
	}
}
